local zhuimeng = fk.CreateSkill{
  name = "hanqing__zhuimeng",
}

Fk:loadTranslationTable{
  ["hanqing__zhuimeng"] = "惴梦",
  [":hanqing__zhuimeng"] = "准备阶段，你可以展示牌堆顶三张牌，然后使用其中的伤害牌。若你未因此造成伤害，则你弃置一张牌。",

  ["#hanqing__zhuimeng-use"] = "惴梦：请使用其中的伤害牌",
}

zhuimeng:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zhuimeng.name) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3)
    room:turnOverCardsFromDrawPile(player, cards, zhuimeng.name)
    if player.dead then
      room:cleanProcessingArea(cards)
      return
    end
    cards = table.filter(cards, function (id)
      return room:getCardArea(id) == Card.Processing
    end)
    if #cards == 0 then return end
    local ids = table.filter(cards, function (id)
      return Fk:getCardById(id).is_damage_card
    end)
    local yes = true
    while not player.dead do
      ids = table.filter(ids, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if #ids == 0 then break end
      local use = room:askToUseRealCard(player, {
        pattern = ids,
        skill_name = zhuimeng.name,
        prompt = "#hanqing__zhuimeng-use",
        extra_data = {
          bypass_times = true,
          extraUse = true,
          expand_pile = ids,
        },
      })
      if use then
        table.removeOne(cards, use.card.id)
        if use.damageDealt then
          yes = false
        end
      else
        break
      end
    end
    if yes and not player.dead then
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = zhuimeng.name,
        cancelable = false,
      })
    end
    room:cleanProcessingArea(cards)
  end,
})

return zhuimeng
